1. Field
Dynamic binary translation of non-native code.
2. Background
A sequence of machine executable code or instructions, such as a computer program are typically written in a “language” such as an instruction set of a processor (e.g., part or all of the “machine”). A language may be all of the operations (jump, store, etc.) that a processor can or is able to perform. In other words, a single processor (embedded or otherwise) can usually only execute a given set of instructions (e.g., language). Normally, a program is developed in a higher-level language (e.g. C or C++) and then compiled to produce an executable file that will run on a target processor. If it is desired to run the program on a different processor, the higher-level language may be compiled with a different compiler to produce a different executable file for the second processor.
However, in some circumstances, the higher-level program may not be available; only the compiled program is available. In those circumstances, the compiled program, or regions of instructions thereof, may be either interpreted (the second processor emulates the original processor and calculates what the original processor would have done in response to each region of instruction) or translated (the region of instructions for the original processor are replaced with a region of instructions or instruction sequence that causes the second processor to perform the same operations that the original processor would have done). Typically, a program includes multiple “branches” or branch instructions delineating subroutines or “blocks” of instructions. A region of instructions may be one or more blocks of instructions. Thus, a region of instructions may be a sequence of one or more instructions or blocks of a program to be interpreted, translated, processed, and/or executed in succession in a dynamic binary translation (DBT) environment or by a dynamic binary translator.
For instance, in a DBT environment, a sequence of instructions or a computer program having instructions in one language (e.g., non-native instructions that can be executed or processed by a non-native machine or processor) may be “executed” or processed by a native machine or processor capable of processing another different language (e.g., such as a native processor for native instructions). Specifically, regions of instructions in the non-native language (e.g., “non-native” instructions) may be “dynamically” interpreted, and other regions may be translated into regions of instructions of the native language for processing by the native processor (e.g., such as by being compiled just-in-time during “execution” in the dynamic binary translation (DBT) environment).
Thus, compiled non-native instructions may be received in a DBT environment for processing by a processor other than the “non-native” processor for which they were compiled. Specifically, a DBT environment may exist where the non-native source code or higher-level program is not available (e.g., only the compiled non-native program is available). Also, a DBT environment may exist where a static compiler is unable to compile the non-native code into compiled native code. Next, a DBT environment may exist where, in a program or instruction sequence, regions of instructions are to be translated and regions of instructions are to be interpreted by a native processor. In this third case, a dynamic binary translator may cause some regions of non-native code instructions (e.g., instructions to control a processor capable of processing one type of code or language) to be translated to regions of native code instructions (e.g., instructions to control a different processor capable of processing a different type of code or language) and may cause other regions of instructions to be interpreted (e.g., the non-native instructions may be processed by the different processor). The goal of interpreting is to have the second processor emulate the original processor, calculate what task the original processor would have done in response to each region of instruction, and perform that task. The goal of translating is to replace the instructions the original processor is able to execute with instruction that the second processor is capable of executing.
For example, “hot regions” or hot code of a sequence of instruction may be small code regions of a program executed frequently at run time and all of the hot code may consume 90% of processor clocks (e.g., clocks of a central processing unit (CPU)) for processing the entire sequence. In a DBT environment non-native hot code regions may be “executed” by being translated into compiled native instructions “just-in-time” before execution or use by the native processor. The first time they are compiled, the non-native regions of hot instructions and the just-in-time compiled native regions of hot instructions are stored in a memory (e.g., in table or, code cache) for quick reuse to translate the non-native hot region to a native version next time that non-native region of instructions is encountered. However, this requires enough memory to store the non-native and the native regions of instructions in a memory/table for reuse. This memory requirement is tolerable because of the relatively high frequency of encountering or executing the regions of hot code and the relatively small portion or succession of the sequence that is the regions of hot code (e.g., it is more desirable to require more memory than to execute the hot code more slowly by using interpretation).
Correspondingly, “cold regions” or cold code in a sequence of instruction may be small code regions of a program executed infrequently at run time and consume few of processor clocks for processing the sequence. Thus, cold code or regions may be “executed” by being interpreted in a DBT environment. For instance, compiled non-native cold code regions can be interpreted into the functionality of native instructions during execution by the native processor. The extra time required to perform each interpretation is tolerable because of the relatively low frequency of encountering or executing the regions of cold code and the relatively large portion or succession of the sequence that is the regions of cold code (e.g., it is more desirable to require more time to interpret and less memory).